﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WorkFlowSystem.DataBase;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;
using WorkFlowSystem.Common;

namespace WorkFlowSystem.Business
{
    class BIZDptInfo
    {
        public void getDptData(bool isInitFlag, ref DataTable dt, ref int dataCnt)
        {
            if (isInitFlag)
            {
                //初始化时显示15条空记录
                for (int i = 0; i < 15; i++)
                {
                    dt.Rows.Add(dt.NewRow());
                }
            }
            else
            {
                DBOperator db_op = new DBOperator();
                if (db_op.ConnectDB() < 0)
                {
                    MessageBox.Show("无法连接服务器，请检查网络连接");
                }
                else
                {
                    string sql = "select a.did, a.d_name, a.d_uid, a.d_parent, b.d_name as pd_name, u.u_name as d_u_name " +
                        "from department a left join department b on a.d_parent=b.did left join user_info u on a.d_uid=u.uid";
                    if (db_op.ExecuteDataTable(sql, null, false, ref dt) < 0)
                    {
                        Logger.WriteLogError("get department info fail");
                    }
                    db_op.CloseDB();

                    if (dt.Rows.Count <= 0)
                    {
                        Logger.WriteLog("no department has been created yet");
                        MessageBox.Show("没有部门信息");
                        dt.Clear();
                    }
                }
                dataCnt = dt.Rows.Count;
                int dtRowCount = dt.Rows.Count;
                //查询数据如果不满15条，补充数据到15条。
                if (dtRowCount < 15)
                {
                    for (int i = 0; i < 15 - dtRowCount; i++)
                    {
                        dt.Rows.Add(dt.NewRow());
                    }
                }
            }
            return;
        }
    }
}
